
CXX = clang++
LD  = $(CXX)
CXXFLAGS = -std=gnu++14 -Wall -Wextra -march=native -O3
LDFLAGS  = -lGL -lSDL2




.PHONY: all clean cleanall test rebuild analyze commit

all: canvas
clean:
	@echo -e "\e[0;32mremoving \e[1;35mobjects\e[0m..."
	rm -f *.o
	@echo -e "\e[0;32mremoving \e[1;35mgenerated source code\e[0m..."
	rm -f *.gen.cc
	@echo -e "\e[0;32mremoving \e[1;35mapi traces\e[0m..."
	rm -f *.trace
cleanall: clean
	@echo -e "\e[0;32mremoving \e[1;35mexecutables\e[0m..."
	rm -f canvas
test: all
	@echo -e "\e[0;32mtesting \e[1;35mcanvas\e[0m..."
	@#MESA_GLSL=dump ./canvas   # dump IR of the GLSL code
	@#cgc -oglsl -profile gp4fp *.fs   # dump ASM of the fragment shader
	@#cgc -oglsl -profile gp4vp *.vs   # dump ASM of the   vertex shader
	LIBGL_SHOW_FPS=1 MESA_DEBUG=FP ./canvas
rebuild: cleanall all
analyze: cleanall
	@echo -e "\e[0;32mstatic analyzing" "\e[1;35mcanvas\e[0m"
	scan-build -V --use-c++=clang++ make
	@echo -e "\e[0;32mapi analyzing" "\e[1;35mcanvas\e[0m"
	apitrace trace -o canvas.trace ./canvas || true
	glretrace canvas.trace
commit: cleanall
	@echo -e "\e[0;32mcommiting " "\e[1;35mcanvas\e[0m"
	git add .
	git diff --cached
	git commit || true




canvas: gl.o canvas.o shader.gen.o
	@echo -e "\e[0;32mlinking \e[1;35mcanvas\e[0m..."
	$(LD) -o $@ $^ $(LDFLAGS)
shader.gen.cc: shader/dot.vs shader/dot.gs shader/dot.fs
	@echo -e "\e[0;32mgenerating \e[1;35mshader.gen.cc\e[0m..."
	@echo "#include \"shader.hh\"" > $@
	@echo >> $@
	@echo "namespace shader" >> $@
	@echo "{" >> $@
	@#####################################################################
	@echo -e "\e[0;32m:: \e[0;34mprocessing \e[1;36mdot \e[1;35mvertex shader\e[0m..."
	@echo "	char dot_vs[] = {" >> $@
	@cat shader/dot.vs | xxd -i >> $@
	@echo "	, 0x00 };" >> $@
	@
	@echo -e "\e[0;32m:: \e[0;34mprocessing \e[1;36mdot \e[1;35mgeometry shader\e[0m..."
	@echo "	char dot_gs[] = {" >> $@
	@cat shader/dot.gs | xxd -i >> $@
	@echo "	, 0x00 };" >> $@
	@
	@echo -e "\e[0;32m:: \e[0;34mprocessing \e[1;36mdot \e[1;35mfragment shader\e[0m..."
	@echo "	char dot_fs[] = {" >> $@
	@cat shader/dot.fs | xxd -i >> $@
	@echo "	, 0x00 };" >> $@
	@#####################################################################
	@echo "}" >> $@
	@echo >> $@

